//
//  HistoryOrderRecommenderViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/30.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit
import RxSwift

class HistoryOrderRecommenderViewController: UIViewController {
    
    var orderVM: OrderViewModel?
    
    let disposeBag = DisposeBag()
    
    var order: UserOrderDetail?
    
    var scrollView: UIScrollView?
    
    //    var mainRole: RoleInOrder?
    
    var role: [RoleInOrder]?
    
    var showPayment = false //是否到能展示支付凭证的阶段
    
    var showCashBack = false //是否到能展示返現詳情的階段
    
    var canEdit = false //经理人不能编辑订单
    
    let orderNumber: OrderInfoBar = {
        let bar = OrderInfoBar()
        bar.titleLabel.text = LanguageHelper.getString(key: "ORDER_NUMBER")
        bar.valueLabel.text = LanguageHelper.getString(key: "NO_RESULT")
        bar.pointerLabel.alpha = 0
        return bar
    }()
    
    let orderStatus: OrderInfoBar = {
        let bar = OrderInfoBar()
        bar.titleLabel.text = LanguageHelper.getString(key: "ORDER_STATUS")
        bar.valueLabel.text = LanguageHelper.getString(key: "NO_RESULT")
        bar.pointerLabel.alpha = 0
        return bar
    }()
    
    let cashBack: OrderInfoBar = {
        let bar = OrderInfoBar()
        bar.titleLabel.text = LanguageHelper.getString(key: "CASHBACK_DETAIL")
        bar.valueLabel.text = ""
        return bar
    }()
    
    var cdView: UIView?
    
    var barNeedToAdjust: [OrderInfoBar] = []
    
    var cdExpanded: Bool = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = lightSaperatorColor
        
        self.navigationItem.title = order?.hotel
        
        checkStatus()
        
        setBackBarItem()
        
        if order?.orderStatus == "TO_BE_REVIEWD" && canEdit {
            let rightBt = UIButton()
            rightBt.contentHorizontalAlignment = .right
            rightBt.setTitle(LanguageHelper.getString(key: "GIVE_REVIEW"), for: .normal)
            rightBt.setTitleColor(doneItemButtonColor, for: .normal)
            rightBt.addTarget(self, action: #selector(submit(_:)), for: .touchUpInside)
            rightBt.sizeToFit()
            self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightBt)
        }
        scrollView = UIScrollView()
        scrollView?.backgroundColor = lightSaperatorColor
        scrollView?.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        scrollView?.showsVerticalScrollIndicator = false
        scrollView?.showsHorizontalScrollIndicator = false
        scrollView?.isScrollEnabled = true
        scrollView?.alwaysBounceVertical = true
        scrollView?.autoresizingMask = UIViewAutoresizing.flexibleHeight
        
        orderNumber.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: menuHeight)
        orderStatus.frame = CGRect(x: 0, y: menuHeight, width: self.view.bounds.width, height: menuHeight)
        cashBack.frame = CGRect(x: 0, y: menuHeight * 2, width: self.view.bounds.width, height: menuHeight)
        
        initOrderValue()
        
        self.scrollView?.addSubview(orderNumber)
        self.scrollView?.addSubview(orderStatus)
        self.scrollView?.addSubview(cashBack)
        
        let cbTapGR = UITapGestureRecognizer()
        cbTapGR.addTarget(self, action: #selector(tapCashback(_:)))
        cashBack.addGestureRecognizer(cbTapGR)
                
        scrollView?.contentSize = CGSize(width: self.view.bounds.width, height: cashBack.frame.origin.y + cashBack.frame.height)
        self.view.addSubview(scrollView!)
        
        initVM()
    }
    
    func submit(_ sender: UIBarButtonItem) {
        let ratingView = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "RatingViewController") as! RatingViewController
        ratingView.orderVM = self.orderVM
        ratingView.orderId = self.order?.id
        self.navigationController?.pushViewController(ratingView, animated: true)
    }
    
    func initVM() {
        let authManager = AuthManager.share
        if authManager.hasLogined() {
            if let token = authManager.getToken() as? String {
                orderVM = OrderViewModel(token: token)
            }
        }
    }
    
    func checkStatus() {
        if let status = self.order?.orderStatus {
            if orderSteps[status]! > 1 && orderSteps[status]! < 7 {
                showPayment = true
            }
            if orderSteps[status]! > 3 && orderSteps[status]! < 7 {
                showCashBack = true
            }
        }
        if let role = role {
            if role.contains(.customer) {
                canEdit = true
            }
        }
    }
    
    func tapCashback(_ sender: UIGestureRecognizer) {
        UIView.animate(withDuration: 0.3, animations: { [unowned self] in
            self.cashBack.backgroundColor = UIColor.init(red: 200 / 255.0, green: 200 / 255.0, blue: 200 / 255.0, alpha: 0.5)
            }, completion: { [unowned self] bool in
                self.cashBack.backgroundColor = UIColor.white
                let cashBackPage = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "OrderCashBackViewController") as! OrderCashBackViewController
                cashBackPage.orderId = self.order?.id
                cashBackPage.role = self.role
                self.navigationController?.pushViewController(cashBackPage, animated: true)
        })
    }
    
    func initOrderValue() {
        if let order = order {
            if let on = order.orderNumber {
                orderNumber.valueLabel.text = "\(on)"
            }
            if let status = order.orderStatus {
                let index = orderSteps[status]! - 1
                orderStatus.valueLabel.text = stepNames[index]
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

